package cn.information.view.Tj;

import cn.information.view.Tj.TjUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.sql.*;

public class ProductTjputUI extends TjUI {

    // 数据库配置
    private static final String URL = "jdbc:mysql://localhost:3306/fp?useUnicode=true&characterEncoding=utf-8";
    private static final String USER = "root";
    private static final String PASSWORD = "ljw20040915.";

    public ProductTjputUI() {
        try {
            // 获取父类的确认按钮和下拉框
            Field btnConfirmField = TjUI.class.getDeclaredField("btnConfirm");
            Field typeComboBoxField = TjUI.class.getDeclaredField("typeComboBox");

            btnConfirmField.setAccessible(true);
            typeComboBoxField.setAccessible(true);

            JButton btnConfirm = (JButton) btnConfirmField.get(this);
            JComboBox<String> typeComboBox = (JComboBox<String>) typeComboBoxField.get(this);

            // 设置点击事件
            btnConfirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String selectedType = (String) typeComboBox.getSelectedItem();
                    if (selectedType != null && !selectedType.isEmpty()) {
                        showStatistics(selectedType);
                    } else {
                        JOptionPane.showMessageDialog(ProductTjputUI.this,
                                "请先选择一个商品类型",
                                "提示",
                                JOptionPane.WARNING_MESSAGE);
                    }
                }
            });

        } catch (NoSuchFieldException | IllegalAccessException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "UI初始化失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * 显示商品类型的统计数据
     */
    private void showStatistics(String type) {
        String sql = "SELECT COUNT(*) AS count, SUM(spprice * spnumber) AS total_sales FROM product_info WHERE sptype = ?";

        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, type);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                int count = rs.getInt("count");
                double totalSales = rs.getDouble("total_sales");

                // 格式化输出到控制台
                System.out.printf("商品类型统计表：%n");
                System.out.println("【" + type + "】类商品统计结果");
                System.out.println("+----------------+--------+--------------+");
                System.out.println("|     类型       | 数量   | 商品进货总支出(元) |");
                System.out.println("+----------------+--------+--------------+");
                System.out.printf("| %-14s | %6d | %12.2f |\n", type, count, totalSales);
                System.out.println("+----------------+--------+--------------+");

                // 弹窗显示结果
                String message = String.format(
                        "【%s】类商品统计结果\n\n数量：%d\n商品进货总支出：%.2f 元",
                        type, count, totalSales);

                JOptionPane.showMessageDialog(this, message, "统计信息", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (SQLException ex) {
            System.err.println("数据库操作失败: " + ex.getMessage());
            JOptionPane.showMessageDialog(this,
                    "数据库操作失败: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ProductTjputUI ui = new ProductTjputUI();
            ui.setVisible(true);
        });
    }
}
